package com.ydjk.service.impl;

import com.ydjk.common.core.domain.model.LoginUser;
import com.ydjk.common.utils.DateUtils;
import com.ydjk.common.utils.SecurityUtils;
import com.ydjk.common.utils.file.FileUtils;
import com.ydjk.common.utils.oss.AliyunOSSUtil;
import com.ydjk.domain.JkGroup;
import com.ydjk.domain.JkGroupUser;
import com.ydjk.mapper.JkGroupMapper;
import com.ydjk.service.IJkGroupService;
import com.ydjk.service.IJkGroupUserService;
import com.ydjk.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 推送组管理Service业务层处理
 *
 * @author ydjk
 * @date 2023-08-30
 */
@Service
public class JkGroupServiceImpl implements IJkGroupService {
    @Autowired
    private JkGroupMapper jkGroupMapper;

    @Autowired
    private IJkGroupUserService groupUserService;

    @Value("${ydjk.profile}")
    private String RootPath;

    @Autowired
    private AliyunOSSUtil ossUtil;

    /**
     * 查询推送组管理
     *
     * @param id 推送组管理主键
     * @return 推送组管理
     */
    @Override
    public JkGroup selectJkGroupById(Long id) {
        return jkGroupMapper.selectJkGroupById(id);
    }

    /**
     * 查询推送组管理列表
     *
     * @param jkGroup 推送组管理
     * @return 推送组管理
     */
    @Override
    public List<JkGroup> selectJkGroupList(JkGroup jkGroup) {
        return jkGroupMapper.selectJkGroupList(jkGroup);
    }

    /**
     * 新增推送组管理
     *
     * @param jkGroup 推送组管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertJkGroup(JkGroup jkGroup) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        jkGroup.setCreateUser(loginUser.getUserId());
        jkGroup.setCreateTime(DateUtils.getNowDate());
        int i = jkGroupMapper.insertJkGroup(jkGroup);
        if (jkGroup.getUserList().size() > 0) {
            ArrayList<JkGroupUser> addList = new ArrayList<>();
            List<String> userList = jkGroup.getUserList();
            Date nowDate = DateUtils.getNowDate();
            userList.forEach(userId -> {
                JkGroupUser jkGroupUser = new JkGroupUser();
                jkGroupUser.setGroupId(jkGroup.getId());
                jkGroupUser.setUserId(Long.valueOf(userId));
                jkGroupUser.setCreateTime(nowDate);
                addList.add(jkGroupUser);
            });
            groupUserService.insertList(addList);
        }
        if (i > 0) {
            QRCodeUtil.createCodeToFile(jkGroup.getId().toString(), new File(RootPath), jkGroup.getId().toString() + ".png");
            File qrCodeFile = new File(RootPath + "/" + jkGroup.getId().toString() + ".png");
            String img_path = ossUtil.uploadByFile(qrCodeFile);
            jkGroup.setGroupQrcode(img_path);
            FileUtils.deleteFile(RootPath + "/" + jkGroup.getId().toString() + ".png");
        }
        return jkGroupMapper.updateJkGroup(jkGroup);
    }

    /**
     * 修改推送组管理
     *
     * @param jkGroup 推送组管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateJkGroup(JkGroup jkGroup) {
        List<String> nowList = jkGroup.getUserList();

        JkGroupUser jkGroupUser1 = new JkGroupUser();
        jkGroupUser1.setGroupId(jkGroup.getId());
        List<JkGroupUser> jkGroupUsers = groupUserService.selectJkGroupUserList(jkGroupUser1);

        ArrayList<JkGroupUser> addList = new ArrayList<>();
        Date nowDate = DateUtils.getNowDate();
        if (jkGroupUsers.size() == 0) {
            if (nowList.size() > 0) {
                nowList.forEach(userId -> {
                    JkGroupUser jkGroupUser = new JkGroupUser();
                    jkGroupUser.setGroupId(jkGroup.getId());
                    jkGroupUser.setUserId(Long.valueOf(userId));
                    jkGroupUser.setCreateTime(nowDate);
                    addList.add(jkGroupUser);
                });
                groupUserService.insertList(addList);
            }
        } else {
            if (nowList.size() > 0) {
                //原userId集合
                ArrayList<String> oldList = new ArrayList<>();
                jkGroupUsers.forEach(gu -> {
                    oldList.add(String.valueOf(gu.getUserId()));
                });
                if (oldList.size() > nowList.size()) {
                    //取差集-应删除数据
                    oldList.removeAll(nowList);
                    if (oldList.size() > 0) {
                        for (String userId : oldList) {
                            JkGroupUser jkGroupUser = new JkGroupUser();
                            jkGroupUser.setGroupId(jkGroup.getId());
                            jkGroupUser.setUserId(Long.valueOf(userId));
                            groupUserService.deleteJkGroupUserByGroupIdAndUserId(jkGroupUser);
                        }
                    }
                } else {
                    nowList.removeAll(oldList);
                    //取差集-应新增数据
                    nowList.forEach(userId -> {
                        JkGroupUser jkGroupUser = new JkGroupUser();
                        jkGroupUser.setGroupId(jkGroup.getId());
                        jkGroupUser.setUserId(Long.valueOf(userId));
                        jkGroupUser.setCreateTime(nowDate);
                        addList.add(jkGroupUser);
                    });
                    groupUserService.insertList(addList);
                }
            } else {
                groupUserService.deleteJkGroupUserByGroupId(jkGroup.getId());
            }
        }
        return jkGroupMapper.updateJkGroup(jkGroup);
    }

    /**
     * 批量删除推送组管理
     *
     * @param ids 需要删除的推送组管理主键
     * @return 结果
     */
    @Override
    public int deleteJkGroupByIds(Long[] ids) {
        return jkGroupMapper.deleteJkGroupByIds(ids);
    }

    /**
     * 删除推送组管理信息
     *
     * @param id 推送组管理主键
     * @return 结果
     */
    @Override
    public int deleteJkGroupById(Long id) {
        return jkGroupMapper.deleteJkGroupById(id);
    }

    @Override
    public int joinGroup(JkGroupUser groupUser) {
        JkGroupUser guser = groupUserService.selectByGroupIdAndUserId(groupUser);
        //该用户已存在当前用户组
        if (guser != null) {
            //是否重新加入
            if (groupUser.getIfReJoin() == 1) {
                guser.setCreateTime(DateUtils.getNowDate());
                return groupUserService.updateJkGroupUser(guser);
            }
            return 2;
        }
        groupUser.setCreateTime(DateUtils.getNowDate());
        return groupUserService.insertJkGroupUser(groupUser);
    }
}
